package com.ler.pai.service.impl;

import com.ler.pai.service.GaoDeService;
import com.ler.pai.util.AdCodeUtil;
import com.ler.pai.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author lww
 * @date 2020-01-30 19:04
 */
@Service
@Slf4j
public class GaoDeServiceImpl implements GaoDeService {

	@Value("${gaode_key}")
	private String gaodeKey;

	private static final String LOCATION_REQUEST_URL = "https://restapi.amap.com/v3/place/text?key=gaodeKey&extensions=base&keywords=addressKeyword";

	private static final String WEATHER_REQUEST_URL = "https://restapi.amap.com/v3/weather/weatherInfo?city=cityName&key=gaodeKey&extensions=all";

	private static final String IP_REQUEST_URL = "https://restapi.amap.com/v3/ip?ip=yourIP&key=gaodeKey";

	@Override
	public String getLocationByKeyword(String keyword) {
		String finalUrl = LOCATION_REQUEST_URL.replace("gaodeKey", gaodeKey).replace("addressKeyword", keyword);
		String result = HttpUtils.sendGetHttps(finalUrl, null, null);
		log.info("GaoDeServiceImpl_getLocationByKeyword_result:{}", result);
		return result;
	}

	@Override
	public String getWeather(String cityName) {
		String codeByName = AdCodeUtil.getCodeByName(cityName);
		log.info("GaoDeServiceImpl_getWeather_codeByName:{}", codeByName);
		Assert.isTrue(StringUtils.isNotBlank(codeByName), "获取城市编码失败！");
		String finalUrl = WEATHER_REQUEST_URL.replace("gaodeKey", gaodeKey).replace("cityName", cityName);
		String result = HttpUtils.sendGetHttps(finalUrl, null, null);
		log.info("GaoDeServiceImpl_getWeather_result:{}", result);
		return result;
	}

	@Override
	public String getIPAddress(String ip) {
		String finalUrl = IP_REQUEST_URL.replace("gaodeKey", gaodeKey).replace("yourIP", ip);
		String result = HttpUtils.sendGetHttps(finalUrl, null, null);
		log.info("GaoDeServiceImpl_getIPAddress_result:{}", result);
		return result;
	}
}
